//
//  VCRefund.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/22.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCRefund: VCBase {
    
    // 订单查询
    private lazy var refundTicketView:RefundTicketView = {
        let ui = RefundTicketView()
        ui.frame = CGRect(x: 0, y: realHeight + MARGIN10, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - realHeight * 2)
        return ui
    }()
    
    // 确认退票按钮
    private lazy var refundSubmitView: RefundSubmitView = {
        let ui = RefundSubmitView()
        ui.backgroundColor = COLOR_LIGHT_WHITE2
        ui.frame = CGRect(x: 0, y: SCREEN_HEIGHT - realHeight, width: SCREEN_WIDTH, height: realHeight)
        return ui
    }()
    
    // 是否是退票
    private var isRefoundTicket: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        init_data()
        init_event()
    }
}

extension VCRefund {
    
    private func init_event() -> Void {
        // 待出行航班点击
        refundTicketView.complete = { item in
            self.refundSubmitView.modifyShowTikcetCount = item.count
        }
        
        // 确认按钮点击
        refundSubmitView.complete = {
            // 拼接数据
            let request = NSMutableDictionary()
            request.setValue(self.refundTicketView.selectItemOrderids(), forKey: "orderid")
            // 请求
            self.doPost(api: "refound/order", dict: request) { (dict) in
                self.isRefoundTicket = true
                // 更新选中票的数量
                self.refundSubmitView.modifyShowTikcetCount = 0
                // 刷新视图数据
                self.init_data()
            }
        }
    }
    
    private func init_data() -> Void {
        guard let userid = UserDefaults.standard.value(forKey: SP_USERID) else {
            self.show(text: "本地数据获取失败，请使用账号密码重新登录")
            return
        }
        // 拼接数据
        let request = NSMutableDictionary()
        request.setValue(userid, forKey: SP_USERID)
        request.setValue(0, forKey: SP_ORDERSTATUS)
        // 请求
        self.doPost(api: "search/order", dict: request) { (dict) in
            // 解析数据
            let dict = dict.value(forKey: SP_BODYREQUEST) as! NSDictionary
            if dict.count == 0 {
                if self.isRefoundTicket {
                    self.isRefoundTicket = false
                    self.show(text: "退票成功，退票金额将原路返还")
                }else {
                    self.view.addSubview(self.noDataView)
                }
                self.refundTicketView.isHidden = true
                return
            }
            // 转化订单数据
            var temp = [OrderInfo]()
            if let arrs = dict.value(forKey: "orders") as? NSArray{
                for item in arrs {
                    let order = OrderInfo(dict: item as! [String : Any])
                    temp.append(order)
                }
            }
            self.refundTicketView.orderInfos = temp
            // 转化乘机人数据
            var passengers = [PassengerInfo]()
            if let arrs = dict.value(forKey: "passengers") as? NSArray{
                for item in arrs {
                    let passenger = PassengerInfo(dict: item as! [String : Any])
                    passengers.append(passenger)
                }
            }
            self.refundTicketView.passengers = passengers
            // 重置保存的图片数量
            self.refundTicketView.imgviews = [UIImageView]()
            
            if self.isRefoundTicket {
                self.show(text: "退票成功，退票金额将原路返还")
            }
            // 重置状态
            self.isRefoundTicket = false
        }
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(refundTicketView)
        view.addSubview(refundSubmitView)
    }
}
